<?php
namespace app\business\controller;
use app\business\model\BusinessClassify;
use app\business\model\BusinessLogin;
use think\Controller;
use app\business\controller\Base;
use think\Session;

class Classify extends Base
{
    public function index()
    {
        $username = Session::get('username');

        $count = BusinessLogin::where('username',$username)->find();

        $num=$count['business_id']; //获取到商家的商品列表
        $data = BusinessClassify::where('classify_delete',1)->where('business_id',$num)->order(['classify_order' => 'desc'])->paginate(10);
        $this->assign('data',$data);
        return $this->fetch();
    }

    public function changeStatus($id)
    {
        $info=BusinessClassify::where('id',$id)->find();
        $bool=$info['classify_status']==1? 0 :1;
        BusinessClassify::where('id',$id)->update(['classify_status'=> $bool]);
        return redirect('index');
    }

    public function create()
    {
        return $this->fetch();
    }

    public function store()
    {
         $_POST['classify_delete'] = 1;
         $data=$_POST;
        $username = Session::get('username');

        $count = BusinessLogin::where('username',$username)->find();

        $num=$count['business_id']; //获取到商家的商品列表
        $data['business_id'] = $num;
         BusinessClassify::create($data);
         return redirect('index');
    }

    public function delete($id)
    {
        BusinessClassify::where('id',$id)->update(['classify_delete'=>0]);
        return redirect('index');
    }

    public function edit($id)
    {
        $data= BusinessClassify::where('id',$id)->find();
        $this->assign('id',$id);
        $this->assign('data',$data);
        return $this->fetch('edit');
    }

    public function update()
    {
        $_POST['classify_delete']=1;
        BusinessClassify::update($_POST);
        return redirect('index');

    }
}